package com.yundahh.u8c.util;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yundahh.u8c.entity.base.U8CResult;
import com.yundahh.u8c.entity.base.U8CQueryData;
import com.yundahh.u8c.exception.U8CApiException;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JsonUtils {

    public static String map2json(Map<String,Object> map){
        ObjectMapper mapper=new ObjectMapper();
        String jsonStr= null;
        try {
            jsonStr = mapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return jsonStr;
    }

    public static HashMap json2map(String json){
        ObjectMapper mapper=new ObjectMapper();
        HashMap map= null;
        try {
            map = mapper.readValue(json, HashMap.class);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return map;
    }

    public static <T> T json2Object(String json,Class<T> tClass) {
        ObjectMapper objectMapper=new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
        T t = null;
        try {
            t = objectMapper.readValue(json, tClass);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return t;
    }





    public static String object2Json(Object o){
        ObjectMapper objectMapper=new ObjectMapper();
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        String s = null;
        try {
            s = objectMapper.writeValueAsString(o);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return s;
    }

    /**
     * 解析查询结果
     * @param jsonStr
     * @param dataType
     * @return
     * @param <T>
     * @throws U8CApiException
     * @throws JsonProcessingException
     */
    public static <T> U8CResult<T> parseQuery(String jsonStr, Class<T> dataType) throws U8CApiException, JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
        // 第一次解析外层结构
        U8CResult<String> outerResponse = mapper.readValue(jsonStr,  U8CResult.class);
        if(StringUtils.isNotBlank(outerResponse.getErrorcode())){
            throw new U8CApiException(outerResponse.getErrormsg());
        }

        // 确保 dataType 不为 null
        if (dataType == null) {
            throw new IllegalArgumentException("dataType cannot be null");
        }

        JavaType javaType = mapper.getTypeFactory()
                .constructParametricType(U8CQueryData.class,  dataType);

        // 转换为泛型响应
        U8CResult<T> genericResponse = new U8CResult<>();
        genericResponse.setStatus(outerResponse.getStatus());
        genericResponse.setTaskNumber(outerResponse.getTaskNumber());
        System.out.println("内部data：" + outerResponse.getData());
        if(outerResponse.getData()!=null) {
            U8CQueryData<T> result = mapper.readValue(outerResponse.getData(), javaType);
            genericResponse.setResultData(result);
        }
        return genericResponse;
    }

    public static  U8CResult parseStringData(String jsonStr) throws U8CApiException, JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
        // 第一次解析外层结构
        U8CResult<String> outerResponse = mapper.readValue(jsonStr,  U8CResult.class);
        if(StringUtils.isNotBlank(outerResponse.getErrorcode())){
            throw new U8CApiException(outerResponse.getErrormsg());
        }
        return outerResponse;
    }


    public static <T> U8CResult<T> parseInsert(String jsonStr, Class<T> dataType) throws U8CApiException, JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
        // 第一次解析外层结构
        U8CResult<String> outerResponse = mapper.readValue(jsonStr,  U8CResult.class);
        if(StringUtils.isNotBlank(outerResponse.getErrorcode())){
            throw new U8CApiException(outerResponse.getErrormsg());
        }

        // 确保 dataType 不为 null
        if (dataType == null) {
            throw new IllegalArgumentException("dataType cannot be null");
        }


        // 转换为泛型响应
        U8CResult<T> genericResponse = new U8CResult<>();
        genericResponse.setStatus(outerResponse.getStatus());
        genericResponse.setTaskNumber(outerResponse.getTaskNumber());
        System.out.println("内部data：" + outerResponse.getData());
        if(outerResponse.getData()!=null) {
            JavaType listType = mapper.getTypeFactory()
                    .constructCollectionType(List.class, dataType);
            List<T> result= mapper.readValue(outerResponse.getData(), listType);
            genericResponse.setInsertData(result);
        }
        return genericResponse;
    }
}
